package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.JobVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.ResultVo;
import com.yyzw.activiti.modeler.service.JobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    JobService jobService;
    @Autowired
    @Qualifier("SchedulerFactory")
    SchedulerFactoryBean schedulerFactoryBean;
    @Autowired
    @Qualifier("Scheduler")
    private Scheduler scheduler;

    @RequestMapping("jobPage")
    public ModelAndView jobPage(HttpServletRequest request,ModelAndView modelAndView) throws Exception{

        modelAndView.setViewName("job/jobList");
        return modelAndView;
    }


    @RequestMapping("jobList")
    public DataGridRespVo jobList(HttpServletRequest request, String page, String rows){
        return jobService.getJobList(Integer.parseInt(page),Integer.parseInt(rows));
    }

    @RequestMapping("/addJob")
    public ResponseVo addNewJob(JobVo jobVo,HttpServletRequest request) throws  Exception{
        try {
            if(jobVo.getOldJobGroup()!=null){
                JobKey key = new JobKey(jobVo.getOldJobName(),jobVo.getOldJobGroup());
                schedulerFactoryBean.getScheduler().deleteJob(key);
            }
            Class cls = Class.forName(jobVo.getJobClassName());
            cls.newInstance();
            //构建Job信息
            JobDetail job = JobBuilder.newJob(cls).withIdentity(jobVo.getJobName(), jobVo.getJobGroup())
                    .withDescription(jobVo.getDescription()).build();
            //触发时间点
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(jobVo.getCronExpression());
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger" + jobVo.getJobName(), jobVo.getJobGroup())
                    .startNow().withSchedule(cronScheduleBuilder).build();
            //交有scheduler安排触发
            schedulerFactoryBean.getScheduler().scheduleJob(job,trigger);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseVo(false,"添加失败");
        }
        return new ResponseVo(true,"添加成功");

    }

    @RequestMapping("resumeJob")
    public ResponseVo resumeJob(JobVo jobVo,HttpServletRequest request) throws Exception{

        try {
            JobKey key = new JobKey(jobVo.getJobName(),jobVo.getJobGroup());
            schedulerFactoryBean.getScheduler().resumeJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new ResponseVo(false,"恢复任务失败");
        }

        return new ResponseVo(true,"恢复任务成功");
    }

    @RequestMapping("triggerJob")
    public ResponseVo triggerJob(JobVo jobVo,HttpServletRequest request) throws Exception{
        try {
            JobKey key =new JobKey(jobVo.getJobName(),jobVo.getJobGroup());
            schedulerFactoryBean.getScheduler().triggerJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new ResponseVo(false,"触发任务失败");
        }
        return new ResponseVo(true,"触发任务成功");
    }

    @RequestMapping("pauseJob")
    public ResponseVo pauseJob(HttpServletRequest request,JobVo jobVo) throws Exception{
        try {
            JobKey key = new JobKey(jobVo.getJobName(),jobVo.getJobGroup());
            schedulerFactoryBean.getScheduler().pauseJob(key);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new ResponseVo(false,"停止任务失败");
        }
        return new ResponseVo(true,"停止任务成功");
    }

    @RequestMapping("removeJob")
    public ResponseVo removeJob(HttpServletRequest request,JobVo jobVo) throws Exception{
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobVo.getJobName(),jobVo.getJobGroup());
            //停止触发器
            schedulerFactoryBean.getScheduler().pauseTrigger(triggerKey);
            //移除触发器
            schedulerFactoryBean.getScheduler().unscheduleJob(triggerKey);
            //删除任务
            schedulerFactoryBean.getScheduler().deleteJob(JobKey.jobKey(jobVo.getJobName(),jobVo.getJobGroup()));
            System.out.println("removeJob"+JobKey.jobKey(jobVo.getJobName()));
        } catch (SchedulerException e) {
            e.printStackTrace();
            return new ResponseVo(false,"移除任务失败");
        }
        return new ResponseVo(true,"移除任务成功");
    }


}
